package com.hugoo.rpt.ds;

import com.hugoo.core.Conf;
import com.hugoo.core.db.HSF;
import com.hugoo.core.db.mo.Record;
import com.hugoo.core.db.mo.RecordSet;
import com.hugoo.core.util.TL;

/**
 *
 * @author Lovelock.Chan
 */
public class ImpDataSource extends DataSource {

    private String scod = null;

    public ImpDataSource(String _sid) {
        super(_sid);
    }

    public ImpDataSource(String _sid, String _version, String scod) {
        super(_sid, _version);
        this.scod = scod;
    }

    public ImpDataSource(String _sid, String _version) {
        super(_sid, _version);
    }

    @Override
    public String getRptColumns() {
        if (TL.isNotEmpty(scod)) {
            RecordSet<Record> rs = HSF.query(null, "select IMP_COLUMN from t_sys_imp_filemeta where scod='" + scod + "' ");
            if (rs.next()) {
                return rs.getString("IMP_COLUMN");
            }
        }
        return Conf.getImpProp(_sid, Conf.IMP._imp_table_column);
    }

    @Override
    public String getRptTitle() {
        return Conf.getImpProp(_sid, Conf.IMP._imp_title);
    }

    @Override
    public String getSqlFilterFieldConfig(String key) {
        if (key.endsWith("scod")) {
            return " x__scod=':scod'";
        } else if (key.endsWith("scdate")) {
            return " x__impdt>= to_date(':scdate','yyyy-mm-dd') ";
        } else if (key.endsWith("ecdate")) {
            return " x__impdt< (to_date(':ecdate','yyyy-mm-dd')+1) ";
        }
        return null;
    }

    @Override
    public String getSqlFilterFields() {
        return "scod,scdate,ecdate";
    }

    @Override
    public String getSqlFilterTemplate() {
        return "scod and scdate and ecdate";
    }

    @Override
    public String getSqlParaColumn() {
        return "scod,scdate,ecdate";
    }

    @Override
    public String getSqlTemplate() {
        StringBuilder sb = new StringBuilder();
        String tbn = Conf.getImpProp(_sid, Conf.IMP._imp_table);
        sb.append("select * from ").append(tbn).append(" where x__sid='").append(_sid).append("' ");
        return sb.toString();
    }
}
